package com.inkegz.message.utils

import com.inkegz.message.entity.ConversationEntity
import com.inkegz.message.entity.ConversationEntity_
import com.inkegz.message.entity.MessageEntity
import com.inkegz.message.entity.MessageEntity_
import com.inkegz.message.repo.base.ObjectBoxState
import io.objectbox.Box
import io.objectbox.BoxStore
import io.objectbox.kotlin.boxFor

internal fun ObjectBoxState.conversationBox(): Box<ConversationEntity> = model.boxFor(ConversationEntity::class.java)

internal fun ObjectBoxState.messageBox(): Box<MessageEntity> = model.boxFor(MessageEntity::class.java)

internal fun ObjectBoxState.queryConversation(conversationKey: String) =
    model.queryConversation(conversationKey)

internal fun ObjectBoxState.queryMessage(messageId: Long) =
    model.queryMessage(messageId)

internal fun BoxStore.queryConversation(conversationKey: String) =
    boxFor(ConversationEntity::class).query().equal(ConversationEntity_.conversationKey, conversationKey).build().findFirst()

internal fun BoxStore.queryMessage(messageId: Long) =
    boxFor(MessageEntity::class).query().equal(MessageEntity_.messageId, messageId).build().findFirst()

fun <T>listEqual(first: List<T>, second: List<T>): Boolean{
    //都是空列表，返回相等
    if (first.isEmpty() && second.isEmpty()) return true
    //列表长度不同，返回不相等
    if (first.size != second.size) return false
    //找不到item，返回相等
    val itemFirst = first.firstOrNull {
        !second.contains(it)
    }
    val itemSecond = second.firstOrNull {
        !first.contains(it)
    }
    //同时为null，表示列表相等，否则列表不等
    return (itemFirst == null) && (itemSecond == null)
}